package com.jlt.web.dic;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.jlt.entity.dic.DicEntity;
import com.jlt.entity.dic.DicType;
import com.jlt.service.dic.DicEntityManager;
import com.jlt.service.dic.DicTypeManager;
import com.jlt.web.CrudActionSupport;
import com.jlt.web.util.Struts2ActionUtils;

/**
 * 字典实体管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 * 
 * @author zhoutao
 */
// 定义URL映射对应/dic/dic_entity.action
@Namespace("/dic")
// 定义名为reload的result重定向到dic_entity.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "dic_entity.action", type = "redirect") })
public class DicEntityAction extends CrudActionSupport<DicEntity> {

	private static final String ENTITY_NAME = "字典实体";

	private static final long serialVersionUID = 1L;

	@Autowired
	private DicEntityManager dicEntityManager;
	@Autowired
	private DicTypeManager dicTypeManager;

	// -- 页面属性 --//
	private Long id;
	private Long dicTypeId;
	private DicEntity entity;
	private Page<DicEntity> page = new Page<DicEntity>(15);// 每页15条记录
	private List<DicType> dicTypes;

	// -- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public DicEntity getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		dicTypes = dicTypeManager.getAllDicType();
		if (id != null) {
			entity = dicEntityManager.getDicEntity(id);
		} else {
			entity = new DicEntity();
			entity.setCreatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setCreatedTime(new Date());
			entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setUpdatedTime(new Date());
		}
	}

	// -- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		dicTypes = dicTypeManager.getAllDicType();
		page = dicEntityManager.searchDicEntity(page, filters, dicTypeId);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
		entity.setUpdatedTime(new Date());
		dicEntityManager.saveDicEntity(entity);
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SAVE,
				Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
		Struts2Utils.renderJson(message);
		return null;
	}

	@Override
	public String delete() throws Exception {
		Map<String, String> message ;
		DicEntity de = dicEntityManager.getDicEntity(id);
		if ("SYSTEM".equalsIgnoreCase(de.getCreatedBy())) {
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.FAILURE, ENTITY_NAME);
			message.put("message", "系统字典不能删除!");
		} else {
			try {
				dicEntityManager.deleteDicEntity(id);
				message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
						Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
						Struts2ActionUtils.FAILURE, ENTITY_NAME);
			}
		}
		Struts2Utils.renderJson(message);
		return null;
	}

	// -- 页面属性访问函数 --//
	/**
	 * list页面显示用户分页列表.
	 */
	public Page<DicEntity> getPage() {
		return page;
	}

	public List<DicType> getDicTypes() {
		return dicTypes;
	}

	public Long getDicTypeId() {
		return dicTypeId;
	}

	public void setDicTypeId(Long dicTypeId) {
		this.dicTypeId = dicTypeId;
	}
}
